import axios from 'axios';
import env from '../config/env';

let util = {};
util.title = function (title) {
	title = title ? title : 'iView project by VUE';
	window.document.title = title;
};

const ajaxUrl = env.toString() === 'development' ?
	'http://pt.x.cn' :
	'http://127.0.0.1';


util.ajax = axios.create({
	baseURL: ajaxUrl,
	timeout: 30000,
});

util.http = function (url, method, params = {}) {
	return new Promise((resolve, reject) => {
		axios({
			method: method,
			url: url,
			data: params,
			baseURL: ajaxUrl,
			timeout: 30000
		})
			.then(response => {
				resolve(response.data);
			})
			.catch(error => {
				reject(error);
			});
	})
};

export default util;
